﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using NCI.EasyObjects;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using CMS.BLL.Entity;
using CMS.DAL;

namespace CMS.BLL
{
    public class Banner
    {
        #region Common
        public eBanner Data;

        public Banner()
        {
            Data = new eBanner();
		}

        ~Banner()
        {
		}

		public virtual void Dispose()
        {
		}

        public int DeleteById(int ID)
        {
            int nResult = 0;
            cBanner objDAL = new cBanner();
            try
            {
                objDAL.LoadByPrimaryKey(ID);
                objDAL.MarkAsDeleted();
                objDAL.Save();
                nResult = ID;
            }
            catch
            {
                nResult = 0;
            }
            return nResult;
        }

        public void LoadById(int ID)
        {
            cBanner objDAL = new cBanner();
            objDAL.LoadByPrimaryKey(ID);
            ConvertData(objDAL);
        }

        public int Insert()
        {
            int nResult = 0;
            cBanner objInfor = new cBanner(); 
            try
            {
                objInfor.AddNew();
                this.MappingData(objInfor);
                objInfor.Save();
                nResult = objInfor.BannerID;
            }
            catch
            {
                nResult = 0; 
            }
            return nResult;
        }
        //Update nhung cai ma eBanner Data dang nam giu thong qua cBanner objInfor
        public int Update(int ID)
        {
            int nResult = 0;
            cBanner objInfor = new cBanner();
            try
            {
                objInfor.LoadByPrimaryKey(ID);
                this.MappingData(objInfor);
                objInfor.Save();
                nResult = ID; 
            }
            catch
            {
                nResult = 0; 
            }
            return nResult;
        }

        public int UpdateStatus(int ID, int Status)
        {
            int nResult = 0;
            cBanner objDAL = new cBanner();
            try
            {
                objDAL.LoadByPrimaryKey(ID);
                objDAL.Status = Status;
             
                objDAL.Save();
                nResult = objDAL.BannerID;
            }
            catch
            {
                nResult = 0;
            }
            return nResult;
        }

        public int UpdatePriority(int BannerID, int Priority)
        {
            int nResult = 0;
            cBanner objDAL = new cBanner();
            try
            {
                objDAL.LoadByPrimaryKey(BannerID);
                objDAL.Priority = Priority;

                objDAL.Save();
                nResult = objDAL.BannerID;
            }
            catch
            {
                nResult = 0;
            }
            return nResult;
        }
        private void MappingData(cBanner objInfor)
        {
            objInfor.BannerType = Data.BannerType;
            objInfor.Images = Data.Images;
            objInfor.InitContent1 = Data.InitContent1;
            objInfor.InitContent2 = Data.InitContent2;
            objInfor.InitContentColor = Data.InitContentColor;
            objInfor.InitContentFontSize = Data.InitContentFontSize;
            objInfor.Link = Data.Link;
            objInfor.Priority = Data.Priority;
            objInfor.Status = Data.Status;
            objInfor.Title1 = Data.Title1;
            objInfor.Title2 = Data.Title2;
            objInfor.TitleColor = Data.TitleColor;
            objInfor.TitleFontSize = Data.TitleFontSize;
        }
        private void ConvertData(cBanner objInfor)
        {
            Data.BannerType = objInfor.BannerType;
            Data.Images = objInfor.Images;
            Data.InitContent1 = objInfor.InitContent1;
            Data.InitContent2 = objInfor.InitContent2;
            Data.InitContentColor = objInfor.InitContentColor;
            Data.InitContentFontSize = objInfor.InitContentFontSize;
            Data.Link = objInfor.Link;
            Data.Priority = objInfor.Priority;
            Data.Status = objInfor.Status;
            Data.Title1 = objInfor.Title1;
            Data.Title2 = objInfor.Title2;
            Data.TitleColor = objInfor.TitleColor;
            Data.TitleFontSize = objInfor.TitleFontSize;
        }
        public DataTable GetAll()
        {
            DataTable tbResult = new DataTable();
            cBanner objDAL = new cBanner();
            tbResult = objDAL.GetAll();
            return tbResult;
        } 
         #endregion
    }
}